package poiManagement;

import java.io.File;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

//import model.POI;
import de.uni_karlsruhe.cm_tm.www.schema.poi.POI;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMWriter {
    DocumentBuilderFactory domFactory;

    DocumentBuilder bomBuilder;

    Document doc = null;

    public int write(List<POI> poiList, String path) {
        System.out.println("\nDOM writer started - building document...");
        try {
            domFactory = DocumentBuilderFactory.newInstance();
            bomBuilder = domFactory.newDocumentBuilder();
            doc = bomBuilder.newDocument();
            // create the root element
            doc.createElement("poiList");
            // add the name space to the root element
            Element poilist = doc.createElementNS(
                    "http://www.cm-tm.uni-karlsruhe.de/KCG", "kcg:poiList");
            // add the XMLSchema-instance name space
            poilist.setAttribute("xmlns:xsi",
                    "http://www.w3.org/2001/XMLSchema-instance");
            // add the location of the XML Schema for
            // the namespace http://www.cm-tm.uni-karlsruhe.de/KCG
            poilist.setAttribute("xmlns:xsi",
                    "http://www.cm-tm.uni-karlsruhe.de/KCG POIList.xsd");
            // iterate through the list of POIs
            for (POI p : poiList) {
                // create a poi element
                Element poi = doc.createElement("kcg:poi");
                // add an id to the poi element
                poi.setAttribute("kcg:id", String.valueOf(p.getId()));
                // create a name element
                Element name = doc.createElement("kcg:name");
                name.setTextContent(p.getName());
                poi.appendChild(name);
                // create a category element
                Element category= doc.createElement("kcg:category");
                name.setTextContent(p.getCategory());
                poi.appendChild(category);
                // create a latitude element
                Element latitude = doc.createElement("kcg:latitude");
                latitude.setTextContent(String.valueOf(p.getLatitude()));
                // append it to the poi element
                poi.appendChild(latitude);
                // create a longitude element
                Element longitude = doc.createElement("kcg:longitude");
                longitude.setTextContent(String.valueOf(p.getLatitude()));
                // append it to the poi element
                poi.appendChild(longitude);
                // append the poi to the list
                poilist.appendChild(poi);
            }
            // append the root node to the document
            doc.appendChild(poilist);
            // output the XML
            outputToFile(path);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private void outputToFile(String path) {
        try {
            // set up a transformer
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            // creat a stream to which the document should be written to after
            // transformation
            Result result = new StreamResult(new File(path));
            // a DOMSource wraps the document object before transformation
            DOMSource source = new DOMSource(doc);
            // call the transformation, the resulting output will be written
            // automatically to the file
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        // inform the user the document has been succesfully written
        System.out.println("Output finished - XML document written to:" + path);
    }
}
